package cn.stylefeng.guns.modular.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.SubUseBrowse;
import cn.stylefeng.guns.modular.business.enums.SubUseBrowseExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.SubUseBrowseMapper;
import cn.stylefeng.guns.modular.business.pojo.SubUseBrowseRequest;
import cn.stylefeng.guns.modular.business.service.SubUseBrowseService;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现层
 *
 * @author zyw
 * @date 2021/08/17 15:39
 */
@Service
public class SubUseBrowseServiceImpl extends ServiceImpl<SubUseBrowseMapper, SubUseBrowse> implements SubUseBrowseService {

    @Resource
    private LoginUserApi loginUserApi;
    @Resource
    private SubUseBrowseService subUseBrowseService;

	@Override
    public void add(SubUseBrowseRequest subUseBrowseRequest) {
        SubUseBrowse subUseBrowse = new SubUseBrowse();
        BeanUtil.copyProperties(subUseBrowseRequest, subUseBrowse);
        this.save(subUseBrowse);
    }

    @Override
    public void saveBrowse(String type, String title, Long name ,HttpServletRequest request) {
        if(type.isEmpty()){
            type="";
        }
        if(title.isEmpty()){
            title="";
        }
        if(name.equals("")){
            name=-1l;
        }
        SubUseBrowseRequest subUseBrowseRequest = new SubUseBrowseRequest();
        subUseBrowseRequest.setBrowseTime(DateUtil.now());
        subUseBrowseRequest.setBrowseType(type);
        subUseBrowseRequest.setBrowseTitle(title);
        subUseBrowseRequest.setUserName(name);
        subUseBrowseRequest.setBrowseUrl(request.getRequestURI());
        //判断浏览记录存在否
        LambdaQueryWrapper<SubUseBrowse> queryWrapper = new LambdaQueryWrapper<>();

        String browseTitle = subUseBrowseRequest.getBrowseTitle();
        Long userName = subUseBrowseRequest.getUserName();
        String browseType = subUseBrowseRequest.getBrowseType();
        String browseUrl = subUseBrowseRequest.getBrowseUrl();

        queryWrapper.eq(ObjectUtil.isNotEmpty(browseTitle), SubUseBrowse::getBrowseTitle, browseTitle);
        queryWrapper.eq(ObjectUtil.isNotEmpty(userName), SubUseBrowse::getUserName, userName);
        queryWrapper.eq(ObjectUtil.isNotEmpty(browseType), SubUseBrowse::getBrowseType, browseType);
        queryWrapper.eq(ObjectUtil.isNotNull(browseUrl), SubUseBrowse::getBrowseUrl, browseUrl);
        List<SubUseBrowse> subUseBrowses = this.list(queryWrapper);
        if(subUseBrowses.size() > 0){
            SubUseBrowseRequest subUseBrowseRequest1 = new SubUseBrowseRequest();
            BeanUtil.copyProperties(subUseBrowses.get(0), subUseBrowseRequest1);
            subUseBrowseRequest1.setBrowseTime(DateUtil.now());
            edit(subUseBrowseRequest1);
        }else {
            try {
                SubUseBrowse subUseBrowse = new SubUseBrowse();
                BeanUtil.copyProperties(subUseBrowseRequest, subUseBrowse);
                this.save(subUseBrowse);
            } catch (Exception e) {
                throw new ServiceException(SubUseBrowseExceptionEnum.SUBUSEBROWSE_NOT_EXISTED);
            }
        }
    }

    @Override
    public void del(SubUseBrowseRequest subUseBrowseRequest) {
        SubUseBrowse subUseBrowse = this.querySubUseBrowse(subUseBrowseRequest);
        this.removeById(subUseBrowse.getId());
    }

    @Override
    public void edit(SubUseBrowseRequest subUseBrowseRequest) {
        SubUseBrowse subUseBrowse = this.querySubUseBrowse(subUseBrowseRequest);
        BeanUtil.copyProperties(subUseBrowseRequest, subUseBrowse);
        this.updateById(subUseBrowse);
    }

    @Override
    public SubUseBrowse detail(SubUseBrowseRequest subUseBrowseRequest) {
        return this.querySubUseBrowse(subUseBrowseRequest);
    }

    @Override
    public PageResult<Map<String, Object>> findPage(SubUseBrowseRequest subUseBrowseRequest) {
        HttpServletRequest request = HttpServletUtil.getRequest();
        Long userid;
        if (ObjectUtil.isNotNull(request)) {

            LoginUser loginUser = loginUserApi.getLoginUser();
            userid = loginUser.getUserId();
        } else {
            throw new ServiceException(SubUseBrowseExceptionEnum.REQUEST_EMPTY);
        }
        if(userid != -1l || userid != 0l) {
            LambdaQueryWrapper<SubUseBrowse> queryWrapper = new LambdaQueryWrapper<>();
            String browseTitle = subUseBrowseRequest.getBrowseTitle();
            queryWrapper.like(ObjectUtil.isNotEmpty(browseTitle), SubUseBrowse::getBrowseTitle, browseTitle)
                    .eq(ObjectUtil.isNotEmpty(userid), SubUseBrowse::getUserName, userid)
                    .select(SubUseBrowse::getBrowseTitle,SubUseBrowse::getBrowseType,SubUseBrowse::getBrowseTime,SubUseBrowse::getBrowseUrl)
                    .orderByDesc(SubUseBrowse::getBrowseTime);
            Page<SubUseBrowse> sysRolePage = this.page(PageFactory.defaultPage(), queryWrapper);

            PageResult<SubUseBrowse> subUseBrowsePageResult = PageResultFactory.createPageResult(sysRolePage);

            List<SubUseBrowse> list = subUseBrowsePageResult.getRows();
            PageResult<Map<String, Object>> pageResult = new PageResult<>();
            List<Map<String,Object>> mapList = new ArrayList<>();
            for(SubUseBrowse subUseBrowse : list){
                Map<String, Object> map = new HashMap<String, Object>();
//            map = JSONObject.parseObject(JSONObject.toJSONString(projectScience), Map.class);
                map.put("browseTitle",subUseBrowse.getBrowseTitle());
                map.put("browseType",subUseBrowse.getBrowseType());
                map.put("browseTime",subUseBrowse.getBrowseTime());
                map.put("browseUrl",subUseBrowse.getBrowseUrl());
                mapList.add(map);
            }
            pageResult.setRows(mapList);
            pageResult.setPageNo(subUseBrowsePageResult.getPageNo());
            pageResult.setTotalRows(subUseBrowsePageResult.getTotalRows());
            pageResult.setPageSize(subUseBrowsePageResult.getPageSize());
            pageResult.setTotalPage(subUseBrowsePageResult.getTotalPage());
            return  pageResult;
        }else{
            throw new ServiceException(SubUseBrowseExceptionEnum.SUBUSEBROWSE_NOT_EXISTED);
        }
    }

    @Override
    public List<SubUseBrowse> findList(SubUseBrowseRequest subUseBrowseRequest) {
        LambdaQueryWrapper<SubUseBrowse> wrapper = this.createWrapper(subUseBrowseRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zyw
     * @date 2021/08/17 15:39
     */
    private SubUseBrowse querySubUseBrowse(SubUseBrowseRequest subUseBrowseRequest) {
        SubUseBrowse subUseBrowse = this.getById(subUseBrowseRequest.getId());
        if (ObjectUtil.isEmpty(subUseBrowse)) {
            throw new ServiceException(SubUseBrowseExceptionEnum.SUBUSEBROWSE_NOT_EXISTED);
        }
        return subUseBrowse;
    }

    /**
     * 创建查询wrapper
     *
     * @author zyw
     * @date 2021/08/17 15:39
     */
    private LambdaQueryWrapper<SubUseBrowse> createWrapper(SubUseBrowseRequest subUseBrowseRequest) {
        LambdaQueryWrapper<SubUseBrowse> queryWrapper = new LambdaQueryWrapper<>();

        Long id = subUseBrowseRequest.getId();
        String browseTitle = subUseBrowseRequest.getBrowseTitle();
        Long userName = subUseBrowseRequest.getUserName();
        String browseType = subUseBrowseRequest.getBrowseType();
        String browseTime = subUseBrowseRequest.getBrowseTime();

        queryWrapper.eq(ObjectUtil.isNotNull(id), SubUseBrowse::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(browseTitle), SubUseBrowse::getBrowseTitle, browseTitle);
        queryWrapper.like(ObjectUtil.isNotEmpty(userName), SubUseBrowse::getUserName, userName);
        queryWrapper.like(ObjectUtil.isNotEmpty(browseType), SubUseBrowse::getBrowseType, browseType);
        queryWrapper.eq(ObjectUtil.isNotNull(browseTime), SubUseBrowse::getBrowseTime, browseTime);

        return queryWrapper;
    }



}